package com.army.stock.util;

import com.army.stock.domain.ArmyUser;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * Created by zhangyong on 15/3/4.
 */
public class SessionUtil {

    private final static String ARMY_USER_KEY = "armyUser";

    public static boolean checkPermit(HttpServletRequest request, String permitId) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return false;
        }
        String permits = (String) session.getAttribute("permits");
        if (StringUtils.isEmpty(permits)) {
            return false;
        }
        String[] arr = StringUtils.split(permits, ',');
        for (String temp : arr) {
            if (temp.equals(permitId)) {
                return true;
            }
        }
        return false;
    }

    public static ArmyUser getSessionArmyUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return null;
        }
        ArmyUser armyUser = (ArmyUser) session.getAttribute(ARMY_USER_KEY);
        return armyUser;
    }

}
